﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;

#endregion

namespace Sharp.Infrastructure
{
    /// <summary>
    ///     缓存帮助类
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="datetime"></param>
        public static void Add<T>(string key, T value, int datetime)
        {
            Add<T>(key, value, DateTimeType.Second, datetime, null, CacheItemPriority.NotRemovable);
        }

        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <param name="value"> </param>
        /// <param name="datetimetype"> </param>
        /// <param name="datetime"> </param>
        public static void Add<T>(string key, T value, DateTimeType datetimetype, int datetime)
        {
            Add<T>(key, value, datetimetype, datetime, null, CacheItemPriority.NotRemovable);
        }

        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <param name="value"> </param>
        /// <param name="datetimetype"> </param>
        /// <param name="datetime"> </param>
        /// <param name="file"> </param>
        public static void Add<T>(string key, T value, DateTimeType datetimetype, int datetime, CacheDependency file)
        {
            Add<T>(key, value, datetimetype, datetime, file, CacheItemPriority.NotRemovable);
        }

        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <param name="value"> </param>
        /// <param name="datetimetype"> </param>
        /// <param name="datetime"> </param>
        /// <param name="file"> </param>
        /// <param name="cacheItemPriority"> </param>
        public static void Add<T>(string key, T value, DateTimeType datetimetype, int datetime, CacheDependency file,
            CacheItemPriority cacheItemPriority)
        {
            if (value == null)
            {
                return;
            }
            double seconds = datetime*(int) datetimetype;

            HttpRuntime.Cache.Add(key, value, file, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero, cacheItemPriority,
                null);
        }


        /// <summary>
        ///     获取缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <returns> </returns>
        public static T Get<T>(string key)
        {
            object o = HttpRuntime.Cache.Get(key);
            if (o == null)
            {
                return default(T);
            }
            else
            {
                return (T) o;
            }
        }

        /// <summary>
        ///     获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TryGet<T>(string key, out T value)
        {
            object obj = HttpRuntime.Cache.Get(key);
            if (obj == null)
            {
                value = default(T);
                return false;
            }
            else
            {
                value = (T) obj;
                return true;
            }
        }

        /// <summary>
        ///     清空缓存
        /// </summary>
        public static void Clear()
        {
            HttpRuntime.Close();
        }

        /// <summary>
        ///     移除缓存
        /// </summary>
        /// <param name="key">缓存id</param>
        public static void Remove(string key)
        {
            try
            {
                HttpRuntime.Cache.Remove(key);
            }
            catch
            {
            }
        }

        /// <summary>
        ///     模糊移除缓存
        /// </summary>
        /// <param name="key">缓存id</param>
        public static void RemoveLike(string key)
        {
            try
            {
                List<string> names = new List<string>();
                var ienum = HttpRuntime.Cache.GetEnumerator();
                while (ienum.MoveNext())
                {
                    if (ienum.Key.ToString().StartsWith(key))
                    {
                        names.Add(ienum.Key.ToString());
                    }
                }
                foreach (var v in names)
                {
                    HttpRuntime.Cache.Remove(v);
                }
            }
            catch
            {
            }
        }
    }

    /// <summary>
    ///     缓存时间方式
    /// </summary>
    public enum DateTimeType
    {
        /// <summary>
        ///     秒
        /// </summary>
        Second = 1,

        /// <summary>
        ///     小时
        /// </summary>
        Hour = 3600,

        /// <summary>
        ///     天数
        /// </summary>
        Day = 86400,
    }
}